<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/***
	客户和潜在客户
**/
class Client extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->library('session');
		$this->load->model('client_model');
		$this->load->model('contact_person_model');
		$this->load->model('contact_record_model');
		$this->load->helper('url');
	}
	
	//显示自己的客户和最近两次更新的联系记录
	public function index($type = 0) {
		isset($_SESSION['user_name']) OR exit('请先登录！');
		$user_name = $_SESSION['user_name'];
		$data ['clients'] = $this->client_model->get_all_by_sale();
				
		$data['title'] = $user_name.'所负责的潜在客户表';
		
		//加载模版解析类
		$this->load->library('parser');
		
		$this->load->view('templates/head');
		$this->load->view('templates/menu');
		//type == 0 加载不含范围的表格
		if ($type == 0) {
			$this->parser->parse('clients/list', $data);
		}
		//type == 1 加载含范围的表格
		if ($type == 1) {
			$this->parser->parse('clients/list_scope', $data);
		}
		$this->load->view('templates/foot');
	}
	
	//显示所有客户和最近两次更新的联系记录
	public function list_all($type = 0) {
		isset($_SESSION['user_name']) OR exit('请先登录！');
		$user_position = $_SESSION['user_position'];
		//只有销售经理能使用该功能
		//市场经理也需要看到
		if ($user_position <> 'SAM' && $user_position <> 'MKM') {
			echo "<p>复制地址过来是不能访问滴，回去重新登录吧。</p>";
			echo "<a href='http://";
			echo base_url();
			echo "index.php/welcome/'>重新登录</a>";
			exit;
		}
		
		$data ['clients'] = $this->client_model->get_all();
				
		$data['title'] = '所有潜在客户表';
		
		//加载模版解析类
		$this->load->library('parser');
		
		$this->load->view('templates/head');
		$this->load->view('templates/menu');
		//type == 0 加载不含范围的表格
		if ($type == 0) {
			$this->parser->parse('clients/list', $data);
		}
		//type == 1 加载含范围的表格
		if ($type == 1) {
			$this->parser->parse('clients/list_scope', $data);
		}
		$this->load->view('templates/foot');
	}
	
	
	//显示所有客户名
	public function clients_names() {
		isset($_SESSION['user_name']) OR exit('请先登录！');
		
		$data ['clients'] = $this->client_model->get_name_all();
		$data['title'] = '所有客户名';
		
		//加载模版解析类
		$this->load->library('parser');
		
		$this->load->view('templates/head');
		$this->load->view('templates/menu');
		$this->parser->parse('clients/list_names', $data);
		$this->load->view('templates/foot');
	}
	//新增客户信息
	public function create() {
		isset($_SESSION['user_name']) OR exit('请先登录！');	
		//如果POST传来数据，去insert并跳转新建联系人，需要传入Client_id, client_name, title；
		//否则打开表单
		
		if (isset($_POST['name'])) {
			
			//返回client_id和client_name
			$data = $this->client_model->create();
			
			$data ['contact_persons'] = $this->contact_person_model->get_by_client($data['client_id']);
			$data['title'] = '联系人表';
			
			//加载模版解析类
			$this->load->library('parser');
			
			$this->load->view('templates/head');
			$this->load->view('templates/menu');
			$this->parser->parse('contact_person/form_create', $data);
			$this->parser->parse('contact_person/list', $data);
			$this->load->view('templates/foot');
		}
		else {
			//获取所有客户名
			$data ['clients'] = $this->client_model->get_name_all();
			
			//加载模版解析类
			$this->load->library('parser');
			
			$this->load->view('templates/head');
			$this->load->view('templates/menu');
			$this->parser->parse('clients/form_create', $data);
			$this->load->view('templates/foot');
		}
		
	}
	
	//编辑客户信息
	public function edit($id = 0) {
		isset($_SESSION['user_name']) OR exit('请先登录！');	
		if (isset($_POST['id'])) {
			//去update这个id
			$id = $this->input->post('id');
			$this->client_model->update_all($id);
			
			
			$this->load->view('templates/head');
			$this->load->view('templates/menu');
			echo "<br />";
			echo "<p class='text-center lead'>客户信息修改成功！</p>";
			$this->load->view('templates/foot');
		}
		else {
			//根据id查寻客户信息
			$data  = $this->client_model->get_by_id($id);
						
			//加载模版解析类
			$this->load->library('parser');
			
			$this->load->view('templates/head');
			$this->load->view('templates/menu');
			$this->parser->parse('clients/form_edit', $data);
			$this->load->view('templates/foot');
		}
	}
	
	//校核客户信息,暂时没有用上
	public function check($id = 0) {
		isset($_SESSION['user_name']) OR exit('请先登录！');	
		//如果Get一个id那么修改check为YES，并显示校核成功信息；
		//列出未校核表
		if ($id <> 0) {
			//去update这个id
			$this->client_model->update_check ($id);
		}
		//获取所有未校核客户信息
		$data ['clients'] = $this->client_model->get_uncheck();
		
		
		$data['title'] = '未校核客户信息表';
		
		//加载模版解析类
		$this->load->library('parser');
		
		$this->load->view('templates/head');
		$this->load->view('templates/menu');
		$this->parser->parse('clients/list_uncheck', $data);
		$this->load->view('templates/foot');
	}
	
	//导出CSV文件
	public function export_csv( ) {
	isset($_SESSION['user_name']) OR exit('请先登录！');
	//$user_name = $_SESSION['user_name'];
	//去数据库整合数据，按原来的表格
	$raw_data = $this->contact_record_model->get_all_csv_sale();
	$data = array();
	//合并相同公司，相同公司的联系人、联系记录将追加到上一条记录里面,$i将不变；
	//
	//所有的字段:means, contact_date, status, contact_name, contact_mobile, contact_tel, contact_email, contact_id, contact_position, client_name, client_id, country, contact_by, website, address, client_type, scope
	$i = 0;
	$client_id = 0;
	$contact_id = 0;
	foreach ($raw_data as $data_item) {
		if ($client_id == $data_item['client_id'] and $data_item['client_id'] <> 0) {
			if ($contact_id == $data_item['contact_id']) {
				//增加一个记录
				//如果这是一条空记录那就不要增加记录
				if ($data_item['contact_date']) {
					$data[$i-1]['record'] = $data[$i-1]['record']."\n".$data_item['contact_name'].", ".$data_item['means'].", ".$data_item['contact_date'].", ".$data_item['status'];
				}
			} else {
				//增加联系人和一个记录
				//如果这个联系人是Others，那就不要增加联系人
				//如果这是一条空记录那就不要增加记录
				if ($data_item['contact_name'] <> 'Others') {
					$data[$i-1]['contact'] = $data[$i-1]['contact']."\n".$data_item['contact_name']."/".$data_item['contact_position']." M: ".$data_item['contact_mobile']." P:".$data_item['contact_tel']." E:".$data_item['contact_email'];
				}
				if ($data_item['contact_date']) {
					$data[$i-1]['record'] = $data[$i-1]['record']."\n".$data_item['contact_name'].", ".$data_item['means'].", ".$data_item['contact_date'].", ".$data_item['status'];
				}
			}
		} else {
			$client_id = $data_item['client_id'];
			$contact_id = $data_item['contact_id'];
			
			$data[]=array (
				'contact_by' => $data_item['contact_by'],
				'country' => $data_item['country'],
				'client_name' => $data_item['client_name'],
				'website' => $data_item['website'],
				'address' => $data_item['address'],
				'client_type' => $data_item['client_type'],
				'scope' => $data_item['scope'],
				'contact' => $data_item['contact_name']."/".$data_item['contact_position']." M: ".$data_item['contact_mobile']." P:".$data_item['contact_tel']." E:".$data_item['contact_email'],
				'record' => $data_item['contact_name'].", ".$data_item['means'].", ".$data_item['contact_date'].", ".$data_item['status']
				);
			$i = $i + 1;
		
		}
		
	}
	
	
	
	//加载下载辅助函数
	$this->load->helper('download');
	
	//打开文件
	@$fp=fopen("./temp/client.csv", "w");
	
	//写入数据
	
	$head = array ("负责人", "国家", "客户名", "网址", "地址", "客户类别", "服务范围", "联系方式", "联系记录");
	foreach ($head as $i => $v) {
		$head[$i] = iconv('utf-8', 'gbk', $v);
	}
	fputcsv($fp, $head);
	
	foreach ($data as $data_item){
		$mystring = array( $data_item['contact_by'], $data_item['country'], $data_item['client_name'], $data_item['website'], $data_item['address'], $data_item['client_type'], $data_item['scope'], $data_item['contact'], $data_item['record']);
		foreach ($mystring as $i => $v) {
		$mystring[$i] = iconv('utf-8', 'gbk', $v);
		}
		
		fputcsv($fp, $mystring);
	}
	
	
	
	//关闭文件
	fclose($fp);
	
	//下载这个文件
	force_download('./temp/client.csv', NULL);
	}
	
}